<template>
    <div id="hy-swiper">
      <div class="swiper" ref="swiper" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd">
        <slot></slot>
      </div>

      <slot name="indicator"></slot>

      <div class="indicator">
        <slot name="indicator" v-if="indicatorDots && slideCount>1">
          <div v-for="(item, index) in slideCount" class="indi-item"
               :class="{active: index === currentIndex-1}" :key="index"></div>
        </slot>
      </div>
    </div>
</template>

<script>
	export default {
    name: "Swiper",
    props: {
      // 滚动的间隔
      interval: {
        type: Number,
        default: 3000
      },
      // 滚动动画的时间
      duration: {
        type: Number,
        default: 300
      },
      // 触发滚动的位置
      moveRatio: {
        type: Number,
        default: 0.25
      },
      // 是否显示面板指示点
      indicatorDots: {
        type: Boolean,
        default: true
      }
    },
    data() {
      return {
        slideCount: 0,  // 元素个数
        totalWidth: 0,  // Swiper的宽度
        swiperStyle: {},  // Swiper样式
        currentIndex: 1,  // 当前的index
        scrolling: false,  // 是否正在滚动
      }
    },
    mounted: function () {
      // 操作DOM, 在前后添加Slide
      setTimeout(() => {
        this.handleDom()
        this.startTimer()
      }, 100)
    },
    methods: {
      /**
       * 启动定时器
       */
      startTimer: function () {
        this.playTimer = window.setInterval(() => {
          this.currentIndex++
          this.scrollContent(-this.currentIndex * this.totalWidth)
        }, this.interval)
      },
      /**
       * 停止定时器
       */
      stopTimer: function() {
        window.clearInterval(this.playTimer)
      },
      /**
       * 滚动轮播图
       */
      scrollContent: function (currentPosition) {
        // 滚动开始
        this.scrolling = true
        // 开始滚动动画
        this.swiperStyle.transition = 'transform '+ this.duration + 'ms'
        this.setTransform(currentPosition)
        // 判断滚动到的位置
        this.checkPosition()
        // 滚动完成
        this.scrolling = false
      },
      /**
       * 校验轮播图的位置
       */
      checkPosition: function () {
        window.setTimeout(() => {
          // 校验正确的位置
          this.swiperStyle.transition = '0ms'
          if (this.currentIndex >= this.slideCount + 1) {
            this.currentIndex = 1
            this.setTransform(-this.currentIndex * this.totalWidth)
          } else if (this.currentIndex <= 0) {
            this.currentIndex = this.slideCount
            this.setTransform(-this.currentIndex * this.totalWidth)
          }

          // 结束移动后的回调
          this.$emit('transitionEnd', this.currentIndex-1)
        }, this.duration)
      },
      /**
       * 设置滚动的位置
       */
      setTransform: function (position) {
        this.swiperStyle.transform = `translate3d(${position}px, 0, 0)`
        this.swiperStyle['-webkit-transform'] = `translate3d(${position}px), 0, 0`
        this.swiperStyle['-ms-transform'] = `translate3d(${position}px), 0, 0`
      },
      /**
       * 在Dom前后添加Slide
       */
      handleDom: function () {
        // 获取要操作的元素
        let swiperEl = this.$refs.swiper
        let slidesEls = swiperEl.getElementsByClassName('slide')

        // 保存个数
        this.slideCount = slidesEls.length

        // 如果大于1个, 那么在前后分别添加一个slide
        if (this.slideCount > 1) {
          let cloneFirst = slidesEls[0].cloneNode(true)
          let cloneLast = slidesEls[this.slideCount - 1].cloneNode(true)
          swiperEl.insertBefore(cloneLast, slidesEls[0])
          swiperEl.appendChild(cloneFirst)
          this.totalWidth = swiperEl.offsetWidth
          this.swiperStyle = swiperEl.style
        }

        // 让swiper元素, 显示第一个(目前是显示前面添加的最后一个元素)
        this.setTransform(-this.totalWidth)
      },
      /**
       * 拖动事件的处理
       */
      touchStart: function(e) {
        // 如果正在滚动, 不可以拖动
        if (this.scrolling) {
          return
        }
        // 停止定时器
        this.stopTimer()
        // 保存开始滚动的位置
        this.startX = e.touches[0].pageX
      },
      touchMove: function(e) {
        // 计算出用户拖动的距离
        this.currentX = e.touches[0].pageX
        this.distance = this.currentX - this.startX
        let currentPosition = -this.currentIndex * this.totalWidth
        let moveDistance = this.distance + currentPosition

        // 设置当前的位置
        this.setTransform(moveDistance)
      },
      touchEnd: function(e) {
        // 获取移动的距离
        let currentMove = Math.abs(this.distance)
        // 判断最终的距离
        if (this.distance === 0) {
          return
        } else if (this.distance > 0 && currentMove > this.totalWidth * this.moveRatio) { // 右边移动超过0.5
          this.currentIndex--
        } else if (this.distance < 0 && currentMove > this.totalWidth * this.moveRatio) { // 向左移动超过0.5
          this.currentIndex++
        }

        // 移动到正确的位置
        this.scrollContent(-this.currentIndex * this.totalWidth)

        // 移动完成后重新开启定时器
        this.startTimer()
      },
    }
  }
</script>

<style scoped>
  #hy-swiper {
    overflow: hidden;
    position: relative;
  }

  .swiper {
    display: flex;
  }

  .indicator {
    display: flex;
    justify-content: center;
    position: absolute;
    width: 100%;
    bottom: 8px;
  }

  .indi-item {
    box-sizing: border-box;
    width: 8px;
    height: 8px;
    border-radius: 4px;
    background-color: #fff;
    line-height: 8px;
    text-align: center;
    font-size: 12px;
    margin: 0 5px;
  }

  .indi-item.active {
    background-color: var(--color-tint);
  }
</style>
